package skitur.server;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

import skitur.shared.Commons;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;

public class ConditionsTable {
	public String getDetailsTable(String region){
		String ret = "<table>";
		// ...
		// Get the Datastore Service
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		// The Query interface assembles a query
		Query q = new Query("Conditions");
		//q.addFilter("lastName", Query.FilterOperator.EQUAL, lastNameParam);
		//q.addFilter("height", Query.FilterOperator.LESS_THAN, maxHeightParam);

		// PreparedQuery contains the methods for fetching query results
		// from the datastore
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_YEAR, -7);
		Date newDate = calendar.getTime();
		
		q.addFilter("tripDate", Query.FilterOperator.GREATER_THAN, newDate);
		q.addSort("tripDate", SortDirection.DESCENDING); // ERROR
		
		
		
		PreparedQuery pq = datastore.prepare(q);
		HashMap<String, Integer> regions = new HashMap<String, Integer>();
		
		ret += "<tr><td>" +
		"Trip date" + "</td><td>" +
		"user" + "</td><td>" +
		"conditions" + "</td><td>" +
		"snowType"+ "</td><td>" +
		"avalancheRisk" + "</td><td>" +
		"comment"+ "</td></tr>";
		for (Entity result : pq.asIterable()) {
			String regionName = (String) result.getProperty("region");
			if(regionName.equals(region)){
				SimpleDateFormat sdf =
			          new SimpleDateFormat("MMMM-dd");
			    
			    
				ret += "<tr><td>" +
				
				sdf.format(result.getProperty("tripDate"))+ "</td><td>" +
				result.getProperty("user")+ "</td><td>";
				int cond = ((Long)result.getProperty("conditions")).intValue();
				for(int i = 0; i < cond; ++i){
					ret += "<img src=\"./star.png\" height=\"15\" width=\"15\" />";
				}
				ret += "</td><td>" + result.getProperty("snowType")+ "</td><td>" +
				result.getProperty("avalancheRisk")+ "</td><td>" +
				result.getProperty("comment")+ "</td></tr>";
			}
		}
		return ret + "</table>";
	}
	public String getTable() {
		
		String ret = "<table>";
		// ...
		// Get the Datastore Service
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		// The Query interface assembles a query
		Query q = new Query("Conditions");
		//q.addFilter("lastName", Query.FilterOperator.EQUAL, lastNameParam);
		//q.addFilter("height", Query.FilterOperator.LESS_THAN, maxHeightParam);

		// PreparedQuery contains the methods for fetching query results
		// from the datastore
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_YEAR, -7);
		Date newDate = calendar.getTime();
		
		q.addFilter("tripDate", Query.FilterOperator.GREATER_THAN, newDate);
		q.addSort("tripDate", SortDirection.ASCENDING); // ERROR
		
		
		
		PreparedQuery pq = datastore.prepare(q);
		HashMap<String, Integer> regions = new HashMap<String, Integer>();
		
		for(String s : Commons.regions){
			regions.put(s, 0);
		}

		for (Entity result : pq.asIterable()) {
			String regionName = (String) result.getProperty("region");
			regions.put((String) result.getProperty("region"), 
					((Long)result.getProperty("conditions")).intValue());
		}
		ret += "<tr><td>Pasmo gorskie</td><td>Warunki</td></tr>";
		for (Entry<String, Integer> p : regions.entrySet()){
			ret += "<tr><td><a href=\"./details.jsp?region=" + p.getKey() + "\">" + p.getKey() + "</a></td><td>";
			for(int i = 0; i < p.getValue(); ++i){
				ret += "<img src=\"./star.png\" height=\"15\" width=\"15\" />";
			}
			ret += "</td></tr>";
		}
		return ret + "</table>";
	}
}
